//
//  UUWalletWithdrawDetailVC.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 04/03/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUWalletWithdrawDetailVC.h"
#import "UUWalletManager.h"
#import "UUResourceManager.h"

typedef enum : NSUInteger {
    UUWalletRecordDetailStatusItemViewImageTypeWaiting,
    UUWalletRecordDetailStatusItemViewImageTypeNow,
    UUWalletRecordDetailStatusItemViewImageTypeSuccess,
    UUWalletRecordDetailStatusItemViewImageTypeFail,
    
} UUWalletRecordDetailStatusItemViewImageType;

@interface UUWalletRecordDetailStatusItemView : UIView

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *timeLabel;

- (void)updateStatusWithTitle:(NSString *)title
                         date:(NSDate *)date
                    imageType:(UUWalletRecordDetailStatusItemViewImageType)imageType;

@end

@implementation UUWalletRecordDetailStatusItemView

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.imageView = [UIImageView new];
        [self addSubview:self.imageView];
        [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(0);
        }];
        
        self.titleLabel = [UILabel new];
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.textColor = RGB_COLOR(101, 106, 122);
        [self addSubview:self.titleLabel];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.top.equalTo(self.imageView.mas_bottom).offset(12);
            make.height.mas_equalTo(20);
        }];
        
        self.timeLabel = [UILabel new];
        self.timeLabel.numberOfLines = 2;
        self.timeLabel.font = [UIFont systemFontOfSize:11];
        self.timeLabel.textAlignment = NSTextAlignmentCenter;
        self.timeLabel.textColor = self.titleLabel.textColor;
        [self addSubview:self.timeLabel];
        [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom).offset(6);
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(30);
        }];
    }
    return self;
}

- (void)updateStatusWithTitle:(NSString *)title
                         date:(NSDate *)date
                    imageType:(UUWalletRecordDetailStatusItemViewImageType)imageType {
    self.titleLabel.text = title;
    
    if (date == nil) {
        self.timeLabel.hidden = YES;
    } else {
        self.timeLabel.hidden = NO;
        self.timeLabel.text = [date formatDateByFormatString:@"yyyy.MM.dd\nHH:mm:ss"];
    }
    
    switch (imageType) {
        case UUWalletRecordDetailStatusItemViewImageTypeFail:
            self.imageView.image = [UIImage imageNamed:@"WalletWithdrawDetailStatusFailed"];
            break;
        case UUWalletRecordDetailStatusItemViewImageTypeSuccess:
            self.imageView.image = [UIImage imageNamed:@"WalletWithdrawDetailStatusSuccess"];
            break;
        case UUWalletRecordDetailStatusItemViewImageTypeWaiting:
            self.imageView.image = [UIImage imageNamed:@"WalletWithdrawDetailStatusNone"];
            break;
        case UUWalletRecordDetailStatusItemViewImageTypeNow:
            self.imageView.image = [UIImage imageNamed:@"WalletWithdrawDetailStatusProcessing"];
            break;
        default:
            DKAssert(0);
            break;
    }
}

@end

//////////////////////////////////////////////////////

@interface UUWalletWithdrawDetailVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, weak) IBOutlet UIView *headerView;
@property (nonatomic, weak) IBOutlet UILabel *priceLabel;
@property (nonatomic, weak) IBOutlet UILabel *withdrawableLabel;

@property (nonatomic, weak) IBOutlet UUWalletRecordDetailStatusItemView *statusItemView1;
@property (nonatomic, weak) IBOutlet UUWalletRecordDetailStatusItemView *statusItemView2;
@property (nonatomic, weak) IBOutlet UUWalletRecordDetailStatusItemView *statusItemView3;

@property (nonatomic, strong) UUWalletRecordDetailModel *record;
@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property (nonatomic, copy) NSArray<NSDictionary *> *contents;

@end

@implementation UUWalletWithdrawDetailVC

+ (BOOL)enableTranslucentNavigationBar {
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    DKAssert(self.recordId != nil);
    
    self.title = @"提现进度";
    
    self.headerView.backgroundColor = COMMON_NAVIGATIONBAR_COLOR;
    
    self.withdrawableLabel.textColor = RGB_COLOR(101, 106, 122);
    self.withdrawableLabel.font = [UIFont systemFontOfSize:12];
    self.withdrawableLabel.text = @"提现金额";
    
    self.tableView.backgroundColor = COMMON_BACKGROUND_COLOR;
    
    UIView *border = [UIView new];
    border.hidden = YES;
    border.tag = 1;
    border.backgroundColor = COMMON_TEXT_DARK_COLOR;
    [self.headerView insertSubview:border atIndex:0];
    [border mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(1);
        make.top.equalTo(self.statusItemView1).offset(10);
        make.left.equalTo(self.statusItemView1.mas_centerX);
        make.right.equalTo(self.statusItemView3.mas_centerX);
    }];
    
    ShowIndicator();
    WEAK_SELF();
    [GetWalletManager() asyncFetchRecordDetailWithRecrodId:self.recordId
                                             completeBlock:^(UUWalletRecordDetailModel *detail, NSError *error) {
                                                 HideIndicatorInView(weakSelf.view);
                                                 
                                                 if (error == nil) {
                                                     weakSelf.record = detail;
                                                     [weakSelf setupUI];
                                                 } else {
                                                     ShowTipsOnView(error.localizedDescription, weakSelf.view);
                                                 }
                                             }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupUI {
    [self.headerView viewWithTag:1].hidden = NO;
    
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:[self.record.money toCurrencyWithoutCurrencySymbol]
                                                                                       attributes:@{
                                                                                                    NSForegroundColorAttributeName : [UIColor whiteColor],
                                                                                                    NSFontAttributeName : [UIFont systemFontOfSize:30]
                                                                                                    }];
    
    [attributedText appendAttributedString:[[NSAttributedString alloc] initWithString:@"元"
                                                                           attributes:@{
                                                                                        NSForegroundColorAttributeName : [UIColor whiteColor],
                                                                                        NSFontAttributeName : [UIFont systemFontOfSize:12]
                                                                                        }]];;
    
    self.priceLabel.attributedText = attributedText;
    
    [self updateStatusWithStatus:self.record.status];
//    [self updateStatusWithStatus:UUWalletRecordStatusWaitingForReview];
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [self updateStatusWithStatus:UUWalletRecordStatusPassed];
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            [self updateStatusWithStatus:UUWalletRecordStatusRejected];
//
//            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                [self updateStatusWithStatus:UUWalletRecordStatusWithdrawFailed];
//
//                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                    [self updateStatusWithStatus:UUWalletRecordStatusWithdrawSuccess];
//                });
//            });
//        });
//    });
    
    switch (self.record.accountType) {
        case UUWalletAccountTypeAlipay:
            self.contents = @[
                              @{@"title" : @"支付宝账号", @"content" : self.record.account},
                              @{@"title" : @"真实姓名", @"content" : self.record.name},
                              ];
            break;
        case UUWalletAccountTypeWechatPay:
            self.contents = @[
                              @{@"title" : @"微信号", @"content" : self.record.account},
                              @{@"title" : @"真实姓名", @"content" : self.record.name},
                              ];
            break;
        case UUWalletAccountTypeUnionPay:
            self.contents = @[
                              @{@"title" : @"所属银行", @"content" : self.record.bankName},
                              @{@"title" : @"银行账号", @"content" : self.record.account},
                              @{@"title" : @"银行开户人", @"content" : self.record.name},
                              @{@"title" : @"银行开户行", @"content" : self.record.depositBank},
                              ];
            break;
        default:
            DKAssert(0);
            break;
    }
    
    [self.tableView reloadData];
}

- (void)updateStatusWithStatus:(UUWalletRecordStatus)status {
    [self.statusItemView1 updateStatusWithTitle:@"提交申请"
                                           date:self.record.createTime
                                      imageType:UUWalletRecordDetailStatusItemViewImageTypeSuccess];
    
    
    switch (status) {
        case UUWalletRecordStatusWaitingForReview:
            [self.statusItemView2 updateStatusWithTitle:@"等待审核"
                                                   date:nil
                                              imageType:UUWalletRecordDetailStatusItemViewImageTypeNow];
            [self.statusItemView3 updateStatusWithTitle:@"等待提现"
                                                   date:nil
                                              imageType:UUWalletRecordDetailStatusItemViewImageTypeWaiting];
            break;
        case UUWalletRecordStatusPassed:
            [self.statusItemView2 updateStatusWithTitle:@"审核通过"
                                                   date:self.record.auditSuccessTime
                                              imageType:UUWalletRecordDetailStatusItemViewImageTypeSuccess];
            [self.statusItemView3 updateStatusWithTitle:@"等待提现"
                                                   date:nil
                                              imageType:UUWalletRecordDetailStatusItemViewImageTypeNow];
            break;
        case UUWalletRecordStatusRejected:
            [self.statusItemView2 updateStatusWithTitle:@"审核不通过"
                                                   date:self.record.auditFailureTime
                                              imageType:UUWalletRecordDetailStatusItemViewImageTypeFail];
            [self.statusItemView3 updateStatusWithTitle:@"提现失败"
                                                   date:nil
                                              imageType:UUWalletRecordDetailStatusItemViewImageTypeFail];
            break;
        case UUWalletRecordStatusWithdrawFailed:
            [self.statusItemView2 updateStatusWithTitle:@"审核通过"
                                                   date:self.record.auditSuccessTime
                                              imageType:UUWalletRecordDetailStatusItemViewImageTypeSuccess];
            [self.statusItemView3 updateStatusWithTitle:@"提现失败"
                                                   date:self.record.withdrawFailureTime
                                              imageType:UUWalletRecordDetailStatusItemViewImageTypeFail];
            break;
        case UUWalletRecordStatusWithdrawSuccess:
            [self.statusItemView2 updateStatusWithTitle:@"审核通过"
                                                   date:self.record.auditSuccessTime
                                              imageType:UUWalletRecordDetailStatusItemViewImageTypeSuccess];
            [self.statusItemView3 updateStatusWithTitle:@"提现成功"
                                                   date:self.record.withdrawSuccessTime
                                              imageType:UUWalletRecordDetailStatusItemViewImageTypeSuccess];
            break;
        default:
            break;
    }
    
    [self.tableView beginUpdates];
    if (status == UUWalletRecordStatusWithdrawFailed || status == UUWalletRecordStatusRejected) {
        UILabel *errorLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 35)];
        errorLabel.text = [NSString stringWithFormat:@"%@ %@", GetResourceManager().warningIconText, self.record.remark];
        errorLabel.textColor = [UIColor whiteColor];
        errorLabel.backgroundColor = COMMON_TEXT_RED_COLOR;
        errorLabel.font = [GetResourceManager() iconFontWithSize:12];
        errorLabel.numberOfLines = 2;
        errorLabel.textAlignment = NSTextAlignmentCenter;

        self.tableView.tableHeaderView = errorLabel;
    } else {
        self.tableView.tableHeaderView = nil;
    }
    [self.tableView endUpdates];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.contents.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    NSDictionary *content = self.contents[indexPath.row];
    
    UILabel *titleLabel = (UILabel *)[cell.contentView viewWithTag:1];
    titleLabel.textColor = COMMON_TEXT_LIGHT_COLOR;
    titleLabel.text = content[@"title"];
    
    UILabel *contentLabel = (UILabel *)[cell.contentView viewWithTag:2];
    contentLabel.textColor = COMMON_TEXT_BLACK_COLOR;
    contentLabel.text = content[@"content"];
    
    return cell;
}

@end
